<!DOCTYPE html>
<!--
@license
Copyright (C) 2018 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-thread-list</title>

<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<link rel="import" href="gr-thread-list.html">

<script>void(0);</script>

<test-fixture id="basic">
  <template>
    <gr-thread-list></gr-thread-list>
  </template>
</test-fixture>

<script>
  suite('gr-thread-list tests', () => {
    let element;
    let sandbox;
    let threadElements;

    setup(() => {
      sandbox = sinon.sandbox.create();
      element = fixture('basic');
      element.threads = [
        {
          comments: [
            {
              __path: '/COMMIT_MSG',
              author: {
                _account_id: 1000000,
                name: 'user',
                username: 'user',
              },
              patch_set: 4,
              id: 'ecf0b9fa_fe1a5f62',
              line: 5,
              updated: '2018-02-08 18:49:18.000000000',
              message: 'test',
              unresolved: true,
            },
            {
              id: '503008e2_0ab203ee',
              path: '/COMMIT_MSG',
              line: 5,
              in_reply_to: 'ecf0b9fa_fe1a5f62',
              updated: '2018-02-13 22:48:48.018000000',
              message: 'draft',
              unresolved: false,
              __draft: true,
              __draftID: '0.m683trwff68',
              __editing: false,
              patch_set: '2',
            },
          ],
          patchNum: 4,
          path: '/COMMIT_MSG',
          line: 5,
          rootId: 'ecf0b9fa_fe1a5f62',
          start_datetime: '2018-02-08 18:49:18.000000000',
        },
        {
          comments: [
            {
              __path: 'test.txt',
              author: {
                _account_id: 1000000,
                name: 'user',
                username: 'user',
              },
              patch_set: 3,
              id: '09a9fb0a_1484e6cf',
              side: 'PARENT',
              updated: '2018-02-13 22:47:19.000000000',
              message: 'Some comment on another patchset.',
              unresolved: false,
            },
          ],
          patchNum: 3,
          path: 'test.txt',
          rootId: '09a9fb0a_1484e6cf',
          start_datetime: '2018-02-13 22:47:19.000000000',
          commentSide: 'PARENT',
        },
        {
          comments: [
            {
              __path: '/COMMIT_MSG',
              author: {
                _account_id: 1000000,
                name: 'user',
                username: 'user',
              },
              patch_set: 2,
              id: '8caddf38_44770ec1',
              line: 4,
              updated: '2018-02-13 22:48:40.000000000',
              message: 'Another unresolved comment',
              unresolved: true,
            },
          ],
          patchNum: 2,
          path: '/COMMIT_MSG',
          line: 4,
          rootId: '8caddf38_44770ec1',
          start_datetime: '2018-02-13 22:48:40.000000000',
        },
        {
          comments: [
            {
              __path: '/COMMIT_MSG',
              author: {
                _account_id: 1000000,
                name: 'user',
                username: 'user',
              },
              patch_set: 2,
              id: 'scaddf38_44770ec1',
              line: 4,
              updated: '2018-02-14 22:48:40.000000000',
              message: 'Yet another unresolved comment',
              unresolved: true,
            },
          ],
          patchNum: 2,
          path: '/COMMIT_MSG',
          line: 4,
          rootId: 'scaddf38_44770ec1',
          start_datetime: '2018-02-14 22:48:40.000000000',
        },
        {
          comments: [
            {
              id: 'zcf0b9fa_fe1a5f62',
              path: '/COMMIT_MSG',
              line: 6,
              updated: '2018-02-15 22:48:48.018000000',
              message: 'resolved draft',
              unresolved: false,
              __draft: true,
              __draftID: '0.m683trwff68',
              __editing: false,
              patch_set: '2',
            },
          ],
          patchNum: 4,
          path: '/COMMIT_MSG',
          line: 6,
          rootId: 'zcf0b9fa_fe1a5f62',
          start_datetime: '2018-02-09 18:49:18.000000000',
        },
      ];
      flushAsynchronousOperations();
      threadElements = Polymer.dom(element.root)
          .querySelectorAll('gr-comment-thread');
    });

    teardown(() => {
      sandbox.restore();
    });

    test('draft toggle only appears when logged in', () => {
      assert.equal(getComputedStyle(element.$$('.draftToggle')).display,
          'none');
      element.loggedIn = true;
      assert.notEqual(getComputedStyle(element.$$('.draftToggle')).display,
          'none');
    });

    test('there are five threads by default', () => {
      assert.equal(Polymer.dom(element.root)
          .querySelectorAll('gr-comment-thread').length, 5);
    });

    test('_computeSortedThreads', () => {
      assert.equal(element._sortedThreads.length, 5);
      // Draft and unresolved
      assert.equal(element._sortedThreads[0].thread.rootId,
          'ecf0b9fa_fe1a5f62');
      // unresolved
      assert.equal(element._sortedThreads[1].thread.rootId,
          'scaddf38_44770ec1');
      // unresolved
      assert.equal(element._sortedThreads[2].thread.rootId,
          '8caddf38_44770ec1');
      // resolved and draft
      assert.equal(element._sortedThreads[3].thread.rootId,
          'zcf0b9fa_fe1a5f62');
      // resolved
      assert.equal(element._sortedThreads[4].thread.rootId,
          '09a9fb0a_1484e6cf');
    });

    test('thread removal', () => {
      threadElements[1].fire('thread-discard', {rootId: 'scaddf38_44770ec1'});
      flushAsynchronousOperations();
      assert.equal(element._sortedThreads.length, 4);
      assert.equal(element._sortedThreads[0].thread.rootId,
          'ecf0b9fa_fe1a5f62');
      // unresolved
      assert.equal(element._sortedThreads[1].thread.rootId,
          '8caddf38_44770ec1');
      // resolved and draft
      assert.equal(element._sortedThreads[2].thread.rootId,
          'zcf0b9fa_fe1a5f62');
      // resolved
      assert.equal(element._sortedThreads[3].thread.rootId,
          '09a9fb0a_1484e6cf');
    });

    test('toggle unresolved only shows unressolved comments', () => {
      MockInteractions.tap(element.$.unresolvedToggle);
      flushAsynchronousOperations();
      assert.equal(Polymer.dom(element.root)
          .querySelectorAll('gr-comment-thread').length, 3);
    });

    test('toggle drafts only shows threads with draft comments', () => {
      MockInteractions.tap(element.$.draftToggle);
      flushAsynchronousOperations();
      assert.equal(Polymer.dom(element.root)
          .querySelectorAll('gr-comment-thread').length, 2);
    });

    test('toggle drafts and unresolved only shows threads with drafts and ' +
        'publicly unresolved ', () => {
      MockInteractions.tap(element.$.draftToggle);
      MockInteractions.tap(element.$.unresolvedToggle);
      flushAsynchronousOperations();
      assert.equal(Polymer.dom(element.root)
          .querySelectorAll('gr-comment-thread').length, 2);
    });

    test('modification events are consumed and displatched', () => {
      sandbox.spy(element, '_handleCommentsChanged');
      const dispatchSpy = sandbox.stub();
      element.addEventListener('thread-list-modified', dispatchSpy);
      threadElements[0].fire('thread-changed', {
        rootId: 'ecf0b9fa_fe1a5f62', path: '/COMMIT_MSG'});
      assert.isTrue(element._handleCommentsChanged.called);
      assert.isTrue(dispatchSpy.called);
      assert.equal(dispatchSpy.lastCall.args[0].detail.rootId,
          'ecf0b9fa_fe1a5f62');
      assert.equal(dispatchSpy.lastCall.args[0].detail.path, '/COMMIT_MSG');
    });
  });
</script>
